package com.surekam.mnai.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.surekam.mnai.dao.MobileNumberAttributionDao;
import com.surekam.mnai.entity.LoadPattern;
import com.surekam.mnai.model.MobileNumberAttribution;

/**
 *
 * @author caoxu
 */
@Component
@Transactional
public class MobileNumAttributionServiceImpl implements
		IMobileNumAttributionService {

	@Autowired
	private MobileNumberAttributionDao dao;
	private String messageForError;

	@Override
	public MobileNumberAttribution addAttribution(
			MobileNumberAttribution targetData) throws ServiceException {
		return templateOperationMethod(targetData, "insert");
	}

	@Override
	public MobileNumberAttribution delAttribution(
			MobileNumberAttribution targetData) {
		return templateOperationMethod(targetData, "delete");
	}

	/**
	 * 得到对错误信息的简短提示
	 * @return
	 */
	public String getMessageForError() {
		return messageForError;
	}

	@Override
	public List<MobileNumberAttribution> listAll() {
		try {
			dao.queryAll();
		} catch (DataAccessException e) {
			e.printStackTrace();// debug
			setMessageForError(e.getMessage());
			return null;
		}
		return null;
	}

	@Override
	public List<MobileNumberAttribution> listBySection(String startID, int size) {
		//没有实现
		return null;
	}

	@Override
	public MobileNumberAttribution load(String telNum) {
		if(telNum.length() < 7){
			throw new NumberFormatException("电话号码长度要至少为7");
		}
		if (telNum == null || "".equals(telNum)) {
			setMessageForError("查询的条件为空！");
			return null;
		}
		try {
			if(telNum.length() > 7){
				telNum = telNum.substring(0,7);
			}
			dao.queryByMobileNumber(telNum);
		} catch (DataAccessException e) {
			e.printStackTrace();// debug
			setMessageForError(e.getMessage());
			return null;
		}
		return null;
	}

	@Override
	public List<MobileNumberAttribution> loadsByPattern(String term,
			LoadPattern pattern) {
		if(pattern == null){
			setMessageForError("选取模式为空！");
			return null;
		}
		
		if(term == null || "".equals(term)){
			switch (pattern) {
			case CITY:
				return dao.queryByCity(term);
				
			default:
				setMessageForError("MobileNumberAttribution没有"+pattern+"搜索模式！");
				return null;
			}
		}
		return null;
	}

	public void setMessageForError(String messageForError) {
		this.messageForError = messageForError;
	}

	/**
	 * 对数据进行增删改操作的方法
	 * 
	 * @param targetData
	 *            操作的对象
	 * @param operationName
	 *            要进行的操作 值只能为 "insert","update","delete"
	 * @return
	 */
	private MobileNumberAttribution templateOperationMethod(
			MobileNumberAttribution targetData, String operationName) {
		if (targetData == null) {
			setMessageForError("操作的对象为空！");
			return null;
		}

		boolean insertSucceeful = false; // 用于容错
		try {
			if ("insert".equals(operationName)) {
				dao.insert(targetData);
				insertSucceeful = true;
				return dao.queryByMobileNumber(targetData
						.getMobileNumberSegment());
			}
			if ("update".equals(operationName)) {
				dao.update(targetData);
				return targetData;
			}
			if ("delete".equals(operationName)) {
				dao.deleteByNumberSegment(targetData.getMobileNumberSegment());
				return targetData;
			}

		} catch (DataAccessException e) {
			e.printStackTrace();
			setMessageForError(e.getMessage());

			//对于添加操作执行成功而返回插入之后数据出现错误 返回插入数据，没有成功抛出异常
			if (insertSucceeful) {
				return targetData;
			} else if(!insertSucceeful){
				throw e;
			}
			return null;
		}
		return null;
	}

	@Override
	public MobileNumberAttribution updateAttribution(
			MobileNumberAttribution targetData) {
		return templateOperationMethod(targetData, "update");
	}

}
